<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="editFromFilter" id="editFromId">
    <div class="layui-form-item">
        <label class="layui-form-label required">部门编号</label>
        <div class="layui-input-block">
            <input type="number" name="id" lay-verify="required" lay-reqtext="部门编号不能为空" placeholder="请输入部门编号"
                   value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">父级部门编号</label>
        <div class="layui-input-inline">
            <!-- lay-search=""，可以让下拉框附带搜索功能 -->
            <select name="pid" id="pidSelectId_edit" lay-filter="pid" lay-search="" lay-verify="required"
                    lay-reqtext="父级部门编号不能为空" placeholder="请选择父级部门编号"></select>
            <!--  <input type="text" name="pid" lay-verify="required" lay-reqtext="父级部门编号不能为空" placeholder="请输入父级部门编号" value="" class="layui-input">-->
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">部门名</label>
        <div class="layui-input-block">
            <input type="text" name="title" lay-verify="required" lay-reqtext="部门名不能为空" placeholder="请输入部门名"
                   value="" class="layui-input">

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">备注</label>
        <div class="layui-input-block">
            <input type="text" name="remark" lay-verify="required" lay-reqtext="备注不能为空" placeholder="备注不能为空"
                   value="" class="layui-input">

        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label" required="required">地址</label>
        <div class="layui-input-block">
            <input type="text" name="address" lay-verify="required" lay-reqtext="地址不能为空" placeholder="请输入地址"
                   value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">删除状态</label>
        <div class="layui-input-block">
            <input type="checkbox" name="available" lay-skin="switch"
                   lay-text="启用|禁用">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        // todo layui.sessionData('deptEditData') ===》 从缓存中根据key获取value
        // 回显数据
        var deptData = layui.sessionData('deptData');
        var editData = deptData.edit;

        // ajax调用接口，查询部门信息，生成下拉框
        $.ajax({
            //动态获取下拉选项的接口，返回数据主要是id+name
            url: 'deptSelect',
            dataType: 'json',
            type: 'get',
            success: function (data) {
                console.log(data)
                var deptList = data.data
                let str = "";
                for (let dept of deptList) {
                    // 如果：下拉框中的value 等于 sessionData中的pid，就选中
                    if (dept.id === editData.pid) {
                        //组装数据
                        str += `<option value='${dept.id}' selected="selected">${dept.title}</option>`;
                    } else {
                        //组装数据
                        str += `<option value='${dept.id}'>${dept.title}</option>`;
                    }

                }
                //jquery赋值方式，将拼接的字符串赋值
                $("#pidSelectId_edit").html(str);
                // 重新渲染生效
                form.render();
            }
        });

        /**
         *    页面表单数据回显
         *    form.val("editFromFilter", {....}
         *    参数一：form表单的lay-filter属性值
         *    参数二：json对象，回显的数据
         */
        form.val("editFromFilter", {
            "id": editData.id,
            // "pid": editData.pid,
            "title": editData.title,
            "remark": editData.remark,
            "address": editData.address,
            "available": editData.available === '1' ? false : true
        });

        // 监听提交
        form.on('submit(saveBtn)', function (data) {
            var editData = data.field

            // 处理开关按钮传入到后台的数据
            if (editData.available === 'on') {
                editData.available = '0';
            } else {
                editData.available = '1';
            }

            // ajax请求，调用后台的修改接口
            $.ajax({
                url: 'edit',
                type: 'post',
                contentType: 'application/json;charset=UTF-8',   //告诉服务器，我请求参数的类型的数据
                data: JSON.stringify(data.field),
                dataType: 'json',                                //告诉服务器，我需要什么类型的响应数据
                success: function (data) {

                }
            });

            var index = layer.alert(
                JSON.stringify(data.field),
                {title: '最终的提交信息'},
                function () {
                    // 关闭弹出层
                    layer.close(index);
                    // 关闭编辑页面层
                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(iframeIndex);

                });

            // 刷新table列表
            parent.window.location.reload();

            return false;
        });

    });
</script>
</body>
</html>